using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempInventoriesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempInventoriesDbo : DatabaseObject
        {
            public TempInventoriesDbo()
            {
                Name = "TempInventories";
                GeneratedClassName = "TempInventories";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryCode", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "CategoryCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Description", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Description" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "EAN", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "EAN" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UOM", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "UOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Brand", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Brand" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Manufacturer", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Manufacturer" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Quantity", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Quantity" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Cost", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Cost" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Retail", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Retail" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GP", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GP" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryId" });
              
            }
        }
		
		public static List<TempInventoryRow> Select(Expression<Func<TempInventoryRow, bool>> expression)
        {
            List<TempInventoryRow> rows = new List<TempInventoryRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempInventoryRow>(new TempInventoriesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempInventoryRow> Select()
		{
			List<TempInventoryRow> rows = new List<TempInventoryRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempInventoryRow>(new TempInventoriesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempInventoryRow> PagedSelect(Expression<Func<TempInventoryRow, bool>> filterExpression, Expression<Func<TempInventoryRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempInventoryRow> rows = new List<TempInventoryRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempInventoryRow>(new TempInventoriesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempInventoryRow> TempInventoryList)
        {
            TempInventoryRow row;

            while (reader.Read())
            {
                row = new TempInventoryRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					StoreCode = Util.CastDbValue<string>(reader["StoreCode"]),
					ArticleCode = Util.CastDbValue<string>(reader["ArticleCode"]),
					CategoryCode = Util.CastDbValue<string>(reader["CategoryCode"]),
					Description = Util.CastDbValue<string>(reader["Description"]),
					EAN = Util.CastDbValue<string>(reader["EAN"]),
					UOM = Util.CastDbValue<string>(reader["UOM"]),
					Brand = Util.CastDbValue<string>(reader["Brand"]),
					Manufacturer = Util.CastDbValue<string>(reader["Manufacturer"]),
					Quantity = Util.CastDbValue<decimal?>(reader["Quantity"]),
					Cost = Util.CastDbValue<decimal?>(reader["Cost"]),
					Retail = Util.CastDbValue<decimal?>(reader["Retail"]),
					GP = Util.CastDbValue<decimal?>(reader["GP"]),
					StoreId = Util.CastDbValue<int?>(reader["StoreId"]),
					ArticleId = Util.CastDbValue<int?>(reader["ArticleId"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"])
                };
                                
                TempInventoryList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempInventoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempInventoryRow>(new TempInventoriesDbo(), row);
            }
        }

		public static void Update(TempInventoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempInventoryRow>(new TempInventoriesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempInventoryRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempInventoryRow>(new TempInventoriesDbo(), expression);
            }
        }

	}
}
	
